package com.example.mycloudpicture.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mycloudpicture.annotation.AuthCheck;
import com.example.mycloudpicture.common.BaseResponse;
import com.example.mycloudpicture.common.DeleteRequest;
import com.example.mycloudpicture.common.ResultUtils;
import com.example.mycloudpicture.constant.UserConstant;
import com.example.mycloudpicture.exception.BusinessException;
import com.example.mycloudpicture.exception.ErrorCode;
import com.example.mycloudpicture.exception.ThrowUtils;
import com.example.mycloudpicture.model.dto.user.UserLoginRequest;
import com.example.mycloudpicture.model.dto.user.UserQueryRequest;
import com.example.mycloudpicture.model.dto.user.UserRegisterRequest;
import com.example.mycloudpicture.model.entity.User;
import com.example.mycloudpicture.model.vo.LoginUserVo;
import com.example.mycloudpicture.model.vo.UserVo;
import com.example.mycloudpicture.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;

import static cn.hutool.core.lang.Console.log;
import static com.example.mycloudpicture.common.ResultUtils.success;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private UserService userService;


    // 处理用户注册请求的接口
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        ThrowUtils.throwIf(userRegisterRequest == null, ErrorCode.PARAMS_ERROR);
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        long result = userService.userRegister(userAccount, userPassword, checkPassword);
        return success(result);
    }

    // 处理用户登录请求的接口
    @PostMapping("/login")
    public BaseResponse<LoginUserVo> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userLoginRequest == null, ErrorCode.PARAMS_ERROR);
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        LoginUserVo loginUserVO = userService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(loginUserVO);
    }
    // 获取当前登录用户信息的接口
    @GetMapping("/get/login")
    public BaseResponse<LoginUserVo> getLoginUser(HttpServletRequest request) {
        User LoginUser=userService.getLoginUser(request);
        return ResultUtils.success(userService.getLoginUserVO(LoginUser));
    }

    // 处理用户登出请求的接口
    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        boolean result = userService.userLogout(request);
        return ResultUtils.success(result);
    }

    /**
     * 分页获取用户封装列表（仅管理员）
     *
     * @param userQueryRequest 查询请求参数
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<UserVo>> listUserVoByPage(@RequestBody UserQueryRequest userQueryRequest) {
        ThrowUtils.throwIf(userQueryRequest == null, ErrorCode.PARAMS_ERROR);
        long current=userQueryRequest.getCurrent();
        long pageSize=userQueryRequest.getPageSize();
        Page<User> userPage=userService.page(new Page<>(current,pageSize),userService.getQueryWrapper(userQueryRequest));
        Page<UserVo> userVoPage=new Page<>(current,pageSize,userPage.getTotal());
        List<UserVo> userVoList = userService.getUserVoList(userPage.getRecords());
        userVoPage.setRecords(userVoList);
        log(userPage);
        log(userVoPage);
        return ResultUtils.success(userVoPage);
    }


    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = userService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

}
